const http = require('http');
const fs = require('fs');

// 单向数据流server->client
let server = http.createServer((req, res) => {
  let url = req.url;
  if (url == '/') {
    let data = fs.readFileSync('index.html');
    res.end(data);
  } else if (url == '/sse') {
    res.setHeader("content-type", 'text/event-stream;charset=utf-8');
    // 服务器端定时向客户端推送数据
    setInterval(() => {
      res.write('data: 时间是' + new Date() + '\r\n\r\n');
    }, 1000);
  }
});

server.listen(3000);